﻿using FineUIPro;
using MyDataProcess;
using KPI_SYSTEM.Code;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPI_SYSTEM
{
    public partial class index : PageBase
    {
        public string roleId = "";
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {

                
                LoadMenuData();
                //treeMenu.RefreshIFrame();

                if (Constants.IS_BASE)
                {
                    treeMenu.HideHScrollbar = false;
                    treeMenu.HideVScrollbar = false;
                    treeMenu.ExpanderToRight = false;
                    treeMenu.HeaderStyle = false;
                }
                
            }
        }
        //protected void treeMenu_NodeLazyLoad(object sender, FineUIPro.TreeNodeEventArgs e)
        //{
        //    LoadMenuData();
        //}
        private void LoadMenuData()
        {
            
            userNameBtn.Text = Session["UserName"].ToString();
            DataSet ds = new DataSet();
            ds = GetMenuData();
            ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["REC_ID"], ds.Tables[0].Columns["Parent_ID"]);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (row.IsNull("Parent_ID"))
                {
                    FineUIPro.TreeNode node = new FineUIPro.TreeNode();
                    node.NodeID = row["REC_ID"].ToString().Trim(); ;
                    node.Text = row["Menu_Name"].ToString().Trim(); ;
                    node.NavigateUrl = row["Code_Path"].ToString().Trim(); 
                    node.IconUrl = row["Ico_PaTh"].ToString().Trim();
                    treeMenu.Nodes.Add(node);

                    ResolveSubTree(row, node);
                }
            }
            
        }

        private void ResolveSubTree(DataRow dataRow, FineUIPro.TreeNode treeNode)
        {
            DataRow[] rows = dataRow.GetChildRows("TreeRelation");
            if (rows.Length > 0)
            {
                // 如果是目录，则默认展开
                treeNode.Expanded = true;
                foreach (DataRow row in rows)
                {
                    FineUIPro.TreeNode node = new FineUIPro.TreeNode();
                    node.NodeID = row["REC_ID"].ToString().Trim();
                    node.Text = row["Menu_Name"].ToString().Trim();
                    node.NavigateUrl = row["Code_Path"].ToString().Trim();
                    node.IconUrl = row["Ico_PaTh"].ToString().Trim();
                    treeNode.Nodes.Add(node);

                    ResolveSubTree(row, node);
                }
            }
        }
        private DataSet GetMenuData()
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SQL_Server"].ConnectionString);
            DataProcess dp1 = new DataProcess();
            DataSet ds1 = new DataSet();//创建dataset
            String RoleId = Session["RoleID"].ToString();
            string sqlstr = "";
            if (RoleId != "1")
            {
                sqlstr = @"WITH PATIENT AS
                            (
                            SELECT * FROM MENU_DICT WHERE REC_ID IN (SELECT MENU_ID FROM ROLE_MENU_MAP_DICT WHERE ROLE_ID={0}  )
                            UNION ALL
                            (SELECT A.* FROM MENU_DICT as a INNER JOIN PATIENT as B on A.REC_ID=B.PARENT_ID WHERE A.STATUS=1 )
                            )
                            SELECT DISTINCT * FROM PATIENT ORDER BY SEQ_NO";
                sqlstr = string.Format(sqlstr, RoleId);
            }
            else
            {
                sqlstr = "SELECT * FROM MENU_DICT WHERE STATUS=1 ORDER BY SEQ_NO ";
            }
            conn.Open();
            ds1 = dp1.SqlService_Query(sqlstr, conn);
            conn.Close();
            return ds1;
        }
    }
}